var uuid;
layui.use(['form','layer','upload','qsform'],function(){
    var $ = layui.jquery,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        form = layui.form,
        upload = layui.upload,
        qsform = layui.qsform;

    form.on("submit(submitForm)",function(data){
        // 弹出loading
        var index = layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        let _id = $(".id").val();
        let _message = "图片添加成功";
        let _formdata = qsform.getDataByQsName(data);
        let _formdataJson = JSON.parse(_formdata);
        _formdataJson.uuid = setUuid();
        _formdata = JSON.stringify(_formdataJson);
        if(_id == null || _id == ""){
            if(!($("#previewPicture").html())){
                layer.close(index);
                layer.msg("上传图片不能为空", {
                    time: 2000
                });
                return false;
            }
            // 新增
            $.post("../../pictures/", JSON.parse(_formdata), function(res){

            })
        }else{
            // 更新
            $.ajax({
                url : "../../pictures/",
                type : "PUT",
                data : JSON.parse(_formdata),
                success : function (res) {

                },
                error : function (res) {
                    layer.msg("更新失败");
                    return false;
                }
            });
            _message = "图片修改成功";
        }
        setTimeout(function(){
            layer.close(index);
            layer.msg(_message);
            layer.closeAll("iframe");
            // 刷新父页面
            parent.location.reload();
        },500);
        return false;
    });

    // 是否公开
    form.on('switch(isPublic)', function(data){
        if(data.elem.checked){
            //$("#"+data.elem.attributes["lay-filter"].value).val(1);
            $("input[qs-name='"+data.elem.attributes["lay-filter"].value+"']").val(1);
        }else{
            //$("#"+data.elem.attributes["lay-filter"].value).val(0)
            $("input[qs-name='"+data.elem.attributes["lay-filter"].value+"']").val(0);
        }
    });

    // 是否置顶
    form.on('switch(isTopping)', function(data){
        if(data.elem.checked){
            $("input[qs-name='"+data.elem.attributes["lay-filter"].value+"']").val(1);
        }else{
            $("input[qs-name='"+data.elem.attributes["lay-filter"].value+"']").val(0);
        }
    });

    //拖拽上传
    upload.render({
        elem: '#uploadPicture'
        ,url: WEBAPP_ROOTPATH + "/annex/upload?fkUuid=" + setUuid() + "&module=picture"
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#previewPicture').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
            });
        }
        ,multiple: false
        ,done: function(res){
            console.log(res)
        }
    });
});


function setUuid(){
    if(!uuid){
        uuid = $(".uuid").val();
        if(!uuid){
            uuid = UUIDUtils.uuid();
        }
    }
    return uuid;
}
